package com.tencent.qcloud.tuikit.tuichat.ui.view.message.viewholder;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.Group;

import com.bumptech.glide.Glide;
import com.cqyc.network.getid.GetMyInformationBox;
import com.cqyc.network.http.NConfig;
import com.cqyc.network.utils.Utils;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.bean.message.CustomGoodsMessageBean;
import com.tencent.qcloud.tuikit.tuichat.bean.message.TUIMessageBean;

import java.util.HashMap;
import java.util.Map;

public class CustomGoodsMessageHolder extends MessageContentHolder {
    private Group group;
    private ImageView imageView;
    private TextView title;
    private TextView price;

    public CustomGoodsMessageHolder(View itemView) {
        super(itemView);
        group = itemView.findViewById(R.id.top_group);
        imageView = itemView.findViewById(R.id.order_image);
        title = itemView.findViewById(R.id.order_title);
        price = itemView.findViewById(R.id.order_price);
    }

    public static final String TAG = CustomGoodsMessageHolder.class.getSimpleName();

    @Override
    public int getVariableLayout() {
        return R.layout.custom_goods_message_layout;
    }

    @Override
    public void layoutVariableViews(TUIMessageBean msg, int position) {
        msgArea.setPadding(0, 0, 0, 0);
        msgArea.setBackgroundResource(R.color.transparent);

        CustomGoodsMessageBean messageBean = null;
        if (msg instanceof CustomGoodsMessageBean) {
            messageBean = (CustomGoodsMessageBean) msg;
        }
        if (messageBean == null) {
            return;
        }

        group.setVisibility(View.GONE);

        Glide.with(imageView).load(messageBean.getDataModel().item_img).into(imageView);
        title.setText(messageBean.getDataModel().item_name);
        price.setText(String.valueOf(messageBean.getDataModel().sales_price));
        msgContentFrame.setClickable(true);
        String finalLink = messageBean.getDataModel().web_url;
        msgContentFrame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Map<String, String> map = new HashMap<>();
                map.put("token", GetMyInformationBox.INSTANCE.getToken());
                map.put("app_id", GetMyInformationBox.INSTANCE.getAppId());
                map.put("android_version_code", GetMyInformationBox.INSTANCE.getVersion());
                map.put("platform", "2");

                String path = Utils.Companion.buildUrl(finalLink, map);
                bundle.putString(NConfig.Companion.getWebViewUrl(), path);
                bundle.putBoolean(NConfig.Companion.getWebViewTitleVisible(), false);
                TUICore.startActivity(NConfig.Companion.getWebName(), bundle);
            }
        });
    }
}
